let vue = new Vue({
    el: "#department",
    data(){
        return{
            //基本分页信息
            page:1,
            pageSize:10,
            pageCount: 20,
            totalData: 999,
            startRow:5,
            pageType:'main',
            //分页状态
            pageMain:true,
            pageSearch1:true,
            pageSearch2:true,
            goPage:true,
            pageSwitch:true,
            //筛选按钮属性值
            switchIcons:false,
            //查询内容
            searchContent:'',
            //查询为空提示信息区域显示
            isTip:false,
            //查询选项名称
            searchTitle:'名称',
            //查询为空提示信息
            tip:'',
            //部门信息
            departments: [],
            //新增表单项
            addForm:{
                addName:'',
                addAddress:'',
                addStatus:'1'
            },
            //修改表单项
            updForm:{
                updDepId:'',
                updName:'',
                updAddress:'',
                updStatus:''
            }
        }
    },

    methods:{
        reset(){
            this.switchIcons = false;
            this.searchContent = '';
            this.searchTitle = '名称';
            this.flashSwitch(1);
        },

        //修改部门状态
        updStatus(dep_id,status){
            console.log("修改部门状态~");
            this.pageType = "main";
            axios.post('/dep/updStatus',{
                dep_id : dep_id,
                status : status
            }).then(res=>{
                if (!res.data.status){
                    lightyear.notify(res.data.msg,"danger","500","mdi mdi-checkbox-marked-close");
                }
            })
        },

        //打开修改模态框并填充信息
        toUpd(dep){
            this.updForm.updDepId = dep.dep_id;
            this.updForm.updName = dep.name;
            this.updForm.updAddress = dep.address;
            this.updForm.updStatus = dep.status;
        },

        //切换筛选状态
        getOfStatus:function(){
            this.departments = [];
            console.log("用户部门状态筛选~");
            this.switchIcons=!this.switchIcons;
            if (this.switchIcons){
                this.pageType = 'switch';
                this.page = 1;
                this.getSwitchMsg(this.page);
            }else {
                //解锁著信息分页状态
                this.pageMain = true;
                //刷新数据模式
                this.pageType = 'main';
                this.page = 1;
                this.flashSwitch(1);
            };
        },

        //用户部门状态筛选
        getSwitchMsg(page){
            axios.get('/dep/getOfStatus',{
                params:{
                    status : 1,
                    page : page,
                    pageSize : this.pageSize
                },
            }).then(res=>{
                if (res.data.status){
                    this.departments = res.data.msg.list;
                    this.flashPage(res.data.msg);
                    if (this.pageSwitch) {
                        this.paginationFlash();
                        this.pageSwitch = false;
                    }
                }else {
                    lightyear.notify(res.msg,"danger","500","mdi mdi-checkbox-marked-close");
                }
            })
            console.log("switchTrue")
            console.log("pageSearch1:"+this.pageSearch1);
            console.log("pageSearch2:"+this.pageSearch2);
            console.log("pageSwitch:"+this.pageSwitch);
            console.log("pageMain:"+this.pageMain);
        },

        //添加部门信息
        addMessage(){
            let toGO = 0;
            this.pageType = 'main';
            console.log("添加部门信息~");
            axios.post('/dep/add',{
                name : this.addForm.addName,
                address : this.addForm.addAddress,
                status : this.addForm.addStatus
            }).then(res=>{
                if (res.data.status){
                    lightyear.notify(res.data.msg,"success","500","mdi mdi-checkbox-marked-circle");
                    console.log(this.totalData);
                    console.log(this.pageSize);
                    console.log("取余"+this.totalData%this.pageSize);
                    console.log("取页面"+this.totalData/this.pageSize);
                    this.pageMain = true;
                   if (this.totalData%this.pageSize==0){
                       this.pageCount += 1;
                       this.endRow +=1;
                   }
                   this.flashSwitch(this.endRow);
                }else {
                    lightyear.notify(res.data.msg,"danger","500","mdi mdi-checkbox-marked-close");
                }
            });
            this.addForm = {addStatus : 1};
            console.log("add")
            console.log("pageSearch1:"+this.pageSearch1);
            console.log("pageSearch2:"+this.pageSearch2);
            console.log("pageSwitch:"+this.pageSwitch);
            console.log("pageMain:"+this.pageMain);
        },

        //修改部门信息
        updMessage(){
            console.log("修改部门信息~");
            this.pageType = 'main';
            axios.post('/dep/update',{
                dep_id: this.updForm.updDepId,
                name: this.updForm.updName,
                address: this.updForm.updAddress,
                status: this.updForm.updStatus
            }).then(res=>{
                if (res.data.status){
                    this.pageMain = true;
                    this.flashSwitch(this.page);
                }else {
                    lightyear.notify(res.data.msg,"danger","500","mdi mdi-checkbox-marked-close");
                }
            })
        },

        //搜索栏查询
        search:function(page){
            console.log("正在搜索。。")
            this.pageType = 'search';
            this.pageSearch1 = true;
            this.pageSearch2 = true;
            this.page = 1;
            this.searchMsg(this.page);
            console.log("search");
            console.log("pageSearch1:"+this.pageSearch1);
            console.log("pageSearch2:"+this.pageSearch2);
            console.log("pageSwitch:"+this.pageSwitch);
            console.log("pageMain:"+this.pageMain);
        },

        searchMsg(page){
            axios.get('/dep/getOfCondition',{
                params:{
                    type: this.searchTitle,
                    content: this.searchContent,
                    page : page,
                    pageSize : this.pageSize
                }
            }).then(res=>{
                if (res.data.status){
                    if (res.data.msg.list==''||res.data.msg.list==null){
                        this.departments = res.data.msg.list;
                        this.flashPage(res.data.msg);
                        if (this.pageSearch1){
                            this.paginationFlash();
                            this.pageSearch1 = false;
                        }
                        this.tip = '很抱歉，未找到您需要的信息~'
                    }else {
                        this.departments = res.data.msg.list;
                        this.flashPage(res.data.msg);
                        if (this.pageSearch2){
                            this.paginationFlash();
                            this.pageSearch2 = false;
                        }
                        this.tip = '';
                    }
                }else {
                    lightyear.notify(res.data.msg,"danger","500","mdi mdi-checkbox-marked-close");
                }
                if (this.searchContent =='' && this.searchContent == null){
                    this.pageSearch1 = true;
                    this.pageSearch2 = true;
                }
            })
        },

        //刷新部门信息
        flashSwitch:function(page){

            axios.get('/dep/get',{
                params:{
                    page : page,
                    pageSize : this.pageSize
                }
            }).then(res=>{
                this.departments = res.data.msg.list;
                this.flashPage(res.data.msg);
                this.endRow = res.data.msg.pages;
                if (this.pageMain){
                    this.paginationFlash();
                    this.pageMain = false;
                }
                console.log(this.endRow);
            })
            console.log("flash");
            console.log("pageSearch1:"+this.pageSearch1);
            console.log("pageSearch2:"+this.pageSearch2);
            console.log("pageSwitch:"+this.pageSwitch);
            console.log("pageMain:"+this.pageMain);
        },

        //获取下拉框信
        titleOfName(){
            this.searchTitle = '名称';
        },

        titleOfAddress(){
            this.searchTitle = '地址';
        },

        //刷新分页信息
        paginationFlash(){
            $('#pagination').pagination({
                current: vue.$data.page,
                pageCount: this.pageCount,//总页码
                coping: true,
                isHide: true,
                callback: function (e){
                    vue.$data.page = e.getCurrent();
                    console.log(vue.$data.page);
                }
            });
        },

        //分页信息刷新
        flashPage(data){
            this.tip='';
            this.pageCount = data.pages;
            this.pageSize = data.pageSize;
            this.totalData = data.total;
            this.startRow = data.startRow;
        }
    },
    created(){
        this.flashSwitch(this.page);
    },
    watch:{
        page: function(val) {
            if (this.pageType=='main'){
                this.flashSwitch(val);
            }else if (this.pageType == 'search'){
                this.searchMsg(val); ``
            }else if (this.pageType == 'switch'){
                this.getSwitchMsg(val);
            }
        },
        pageSwitch:function (val){
            if (!val){
                // this.goPage = true;
                this.pageSearch2 = true;
                this.pageSearch1 = true;
                this.pageMain = true;
            }
        },
        pageSearch1:function (val){
            if (!val){
                // this.goPage = true;
                this.pageSearch2 = true;
                this.pageSwitch = true;
                this.pageMain = true;
            }
        },
        pageSearch2:function (val){
            if (!val){
                // this.goPage = true;
                this.pageSearch1 = true;
                this.pageSwitch = true;
                this.pageMain = true;
            }
        },
        // goPage:function (val){
        //     if (!val){
        //         this.pageMain = true;
        //         this.pageSearch1 = true;
        //         this.pageSearch2 = true;
        //         this.pageSwitch = true;
        //     }
        // },
        pageMain:function (val){
            if (!val){
                this.pageSearch1 = true;
                this.pageSearch2 = true;
                this.pageSwitch = true;
                // this.goPage = true;
            }
        },
    },
});
